package codec;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import protocol.Packet;
import util.SerializationUtil;

public class ObjEncoder extends MessageToByteEncoder<Packet> {
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, Packet packet, ByteBuf byteBuf) throws Exception {
        byte[] data = SerializationUtil.serialize(packet);
        // +1，数据包类型
        byteBuf.writeInt(data.length + 1);
        byteBuf.writeByte(packet.getCommand());
        byteBuf.writeBytes(data);
    }
}
